//
//  UIUtil.m
//  Jikeyi
//
//  Created by zhengpeng on 14-4-8.
//  Copyright (c) 2014年 zhengpeng. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "UIUtil.h"


@implementation UIUtil
//image button
+ (UIButton *)drawButtonInView:(UIView *)view frame:(CGRect)frame iconName:(NSString *)iconName target:(id)target action:(SEL)action
{
    UIButton *btn = [self getButtonWithFrame:frame iconName:iconName target:target action:action];
	[view addSubview:btn];
	return btn;
}

+ (UIButton *)getButtonWithFrame:(CGRect)frame iconName:(NSString *)iconName target:(id)target action:(SEL)action
{
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
	btn.frame = frame;
    [btn setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
	[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
	return btn;
}

+ (UIButton *)drawButtonInView:(UIView *)view frame:(CGRect)frame iconName:(NSString *)iconName capInsets:(UIEdgeInsets)capInsets target:(id)target action:(SEL)action
{
    UIButton *btn = [self getButtonWithFrame:frame iconName:iconName capInsets:capInsets target:target action:action];
	[view addSubview:btn];
	return btn;
}

+ (UIButton *)getButtonWithFrame:(CGRect)frame iconName:(NSString *)iconName capInsets:(UIEdgeInsets)capInsets target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
	btn.frame = frame;
	[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
	UIImage *img = [UIImage imageNamed:iconName];
    if ([img respondsToSelector:@selector(resizableImageWithCapInsets:)])
    {
        img = [img resizableImageWithCapInsets:capInsets];
    }else{
        img = [img stretchableImageWithLeftCapWidth:capInsets.left topCapHeight:capInsets.top];
    }
    
    [btn setBackgroundImage:img forState:UIControlStateNormal];
	return btn;
}

+ (UIButton *)drawButtonInView:(UIView *)view frame:(CGRect)frame iconName:(NSString *)iconName target:(id)target action:(SEL)action tag:(NSInteger)tag
{
	UIButton *btn = [self getButtonWithFrame:frame iconName:iconName target:target action:action tag:tag];
	[view addSubview:btn];
	return btn;
}

+ (UIButton *)getButtonWithFrame:(CGRect)frame iconName:(NSString *)iconName target:(id)target action:(SEL)action tag:(NSInteger)tag
{
    UIButton *btn = [self getButtonWithFrame:frame iconName:iconName target:target action:action];
	btn.tag = tag;
	return btn;
}

+ (UIButton *)drawButtonInView:(UIView *)view frame:(CGRect)frame iconName:(NSString *)iconName capInsets:(UIEdgeInsets)capInsets target:(id)target action:(SEL)action tag:(NSInteger)tag
{
	UIButton *btn = [self getButtonWithFrame:frame iconName:iconName capInsets:capInsets target:target action:action tag:tag];
	[view addSubview:btn];
	return btn;
}

+ (UIButton *)getButtonWithFrame:(CGRect)frame iconName:(NSString *)iconName capInsets:(UIEdgeInsets)capInsets target:(id)target action:(SEL)action tag:(NSInteger)tag
{
    UIButton *btn = [self getButtonWithFrame:frame iconName:iconName capInsets:capInsets target:target action:action];
	btn.tag = tag;
	return btn;
}

//text button
+ (UIButton *)drawButtonInView:(UIView *)view frame:(CGRect)frame text:(NSString *)text font:(UIFont *)font color:(UIColor *)color target:(id)target action:(SEL)action
{
    UIButton *btn = [self getButtonWithFrame:frame text:text font:font color:color target:target action:action];
	[view addSubview:btn];
	return btn;
}

+ (UIButton *)getButtonWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font color:(UIColor *)color target:(id)target action:(SEL)action
{
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
	btn.frame = frame;
    [btn setTitle:text forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font = font;
	[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
	return btn;
}

//image text button
+ (UIButton *)drawButtonInView:(UIView *)view frame:(CGRect)frame iconName:(NSString *)iconName capInsets:(UIEdgeInsets)capInsets text:(NSString *)text font:(UIFont *)font color:(UIColor *)color target:(id)target action:(SEL)action
{
    UIButton *btn = [self getButtonWithFrame:frame iconName:iconName capInsets:capInsets text:text font:font color:color target:target action:action];
	[view addSubview:btn];
	return btn;
}

+ (UIButton *)getButtonWithFrame:(CGRect)frame iconName:(NSString *)iconName capInsets:(UIEdgeInsets)capInsets text:(NSString *)text font:(UIFont *)font color:(UIColor *)color  target:(id)target action:(SEL)action
{
    UIButton *btn = [self getButtonWithFrame:frame iconName:iconName capInsets:capInsets target:target action:action];
	[btn setTitle:text forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    btn.titleLabel.textColor = color;
	return btn;
}

//image
+ (UIImageView *)drawCustomImgViewInView:(UIView *)view frame:(CGRect)frame imageName:(NSString *)imageName
{
	UIImageView *iv = [self getCustomImgViewWithFrame:frame imageName:imageName];
	[view addSubview:iv];
    return iv;
}

+ (UIImageView *)getCustomImgViewWithFrame:(CGRect)frame imageName:(NSString *)imageName
{
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
	iv.frame = frame;
    return iv;
}

+ (UIImageView *)drawCustomImgViewInView:(UIView *)view frame:(CGRect)frame imageName:(NSString *)imageName tag:(NSInteger)tag
{
    UIImageView *iv = [self getCustomImgViewWithFrame:frame imageName:imageName tag:tag];
    [view addSubview:iv];
    return iv;
}

+ (UIImageView *)getCustomImgViewWithFrame:(CGRect)frame imageName:(NSString *)imageName tag:(NSInteger)tag
{
    UIImageView *iv = [self getCustomImgViewWithFrame:frame imageName:imageName];
    iv.tag = tag;
    return iv;
}

+ (UIImageView *)drawCustomImgViewInView:(UIView *)view frame:(CGRect)frame imageName:(NSString *)imageName capInsets:(UIEdgeInsets)capInsets
{
    UIImageView *iv = [self getCustomImgViewWithFrame:frame imageName:imageName capInsets:capInsets];
    [view addSubview:iv];
    return iv;
}

+ (UIImageView *)getCustomImgViewWithFrame:(CGRect)frame imageName:(NSString *)imageName capInsets:(UIEdgeInsets)capInsets
{
    UIImageView *iv = [self getCustomImgViewWithFrame:frame imageName:imageName];
    
    UIImage *img = [UIImage imageNamed:imageName];
    if ([img respondsToSelector:@selector(resizableImageWithCapInsets:)])
    {
        img = [img resizableImageWithCapInsets:capInsets];
    }else{
        img = [img stretchableImageWithLeftCapWidth:capInsets.left topCapHeight:capInsets.top];
    }
    
    iv.image = img;
    return iv;
}

+ (UIImageView *)drawCustomImgViewInView:(UIView *)view frame:(CGRect)frame imageName:(NSString *)imageName capInsets:(UIEdgeInsets)capInsets tag:(NSInteger)tag
{
    UIImageView *iv = [self getCustomImgViewWithFrame:frame imageName:imageName capInsets:capInsets tag:tag];
    [view addSubview:iv];
    return iv;
}

+ (UIImageView *)getCustomImgViewWithFrame:(CGRect)frame imageName:(NSString *)imageName capInsets:(UIEdgeInsets)capInsets tag:(NSInteger)tag
{
    UIImageView *iv = [self getCustomImgViewWithFrame:frame imageName:imageName capInsets:capInsets];
    iv.tag = tag;
    return iv;
}

//label
+ (UILabel *)drawLabelInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter
{
	UILabel *lb = [self getLabelWithFrame:frame font:font text:text isCenter:isCenter];
	[view addSubview:lb];
	return lb;
}

+ (UILabel *)getLabelWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter
{
    UILabel *lb = [[UILabel alloc] initWithFrame:frame];
	lb.backgroundColor = [UIColor clearColor];
	lb.font = font;
	lb.text = text;
    if (isCenter) {
        lb.textAlignment = NSTextAlignmentCenter;
    }
	return lb;
}

+ (UILabel *)drawLabelInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter tag:(NSInteger)tag
{
	UILabel *lb = [self getLabelWithFrame:frame font:font text:text isCenter:isCenter tag:tag];
	[view addSubview:lb];
	return lb;
}

+ (UILabel *)getLabelWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter tag:(NSInteger)tag
{
    UILabel *lb = [self getLabelWithFrame:frame font:font text:text isCenter:isCenter];
	lb.tag = tag;
	return lb;
}

+ (UILabel *)drawLabelInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter color:(UIColor *)color
{
	UILabel *lb = [self getLabelWithFrame:frame font:font text:text isCenter:isCenter color:color];
	[view addSubview:lb];
	return lb;
}

+ (UILabel *)getLabelWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter color:(UIColor *)color
{
    UILabel *lb = [self getLabelWithFrame:frame font:font text:text isCenter:isCenter];
	lb.textColor = color;
	return lb;
}

+ (UILabel *)drawLabelMutiLineInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter
{
	UILabel *lb = [self getLabelMutiLineWithFrame:frame font:font text:text isCenter:isCenter];
    [view addSubview:lb];
	return lb;
}

+ (UILabel *)getLabelMutiLineWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter
{
    float height;
    if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        CGRect rect = [text boundingRectWithSize:CGSizeMake(frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];
        height = rect.size.height;
    } else {
        CGSize lblSize = CGSizeZero;
        if (font) {
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
            paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
            NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
            lblSize = [text boundingRectWithSize:CGSizeMake(frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        }
        height = lblSize.height;
    }

	UILabel *lb = [self getLabelWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height) font:font text:text isCenter:isCenter];
	lb.numberOfLines = 0;
	return lb;
}

+ (UILabel *)drawLabelMutiLineInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font text:(NSString *)text  isCenter:(BOOL)isCenter tag:(NSInteger)tag
{
    UILabel *lb = [self getLabelMutiLineWithFrame:frame font:font text:text isCenter:isCenter tag:tag];
	[view addSubview:lb];
	return lb;
}

+ (UILabel *)getLabelMutiLineWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text  isCenter:(BOOL)isCenter tag:(NSInteger)tag
{
    UILabel *lb = [self getLabelMutiLineWithFrame:frame font:font text:text isCenter:isCenter];
	lb.tag = tag;
	return lb;
}

+ (UILabel *)drawLabelMutiLineInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter color:(UIColor *)color
{
    UILabel *lb = [self getLabelMutiLineWithFrame:frame font:font text:text isCenter:isCenter color:color];
	[view addSubview:lb];
	return lb;
}

+ (UILabel *)getLabelMutiLineWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text isCenter:(BOOL)isCenter color:(UIColor *)color
{
    UILabel *lb = [self getLabelMutiLineWithFrame:frame font:font text:text isCenter:isCenter];
	lb.textColor = color;
	return lb;
}

+ (UILabel *)productQoSLabelWithText:(NSString *)text{
    return [self borderedLabelWithText:text font:[TKFont boldSystemFontOfSize:10] color:[UIColor whiteColor] backgroundColor:[UIColor clearColor] borderColor:[UIColor whiteColor] borderWidth:1.0f cornerRadius:0.0f padding:2.0f];
}

+ (UILabel *)borderedLabelWithText:(NSString *)text
                              font:(UIFont *)font
                             color:(UIColor *)color
                   backgroundColor:(UIColor *)backgroundColor
                       borderColor:(UIColor *)borderColor
                       borderWidth:(CGFloat)borderWidth
                      cornerRadius:(CGFloat)cornerRadius
                           padding:(CGFloat)padding
{
    UILabel *label = [self getLabelWithFrame:CGRectZero font:font text:text isCenter:YES color:color];
    label.numberOfLines = 1;
    label.backgroundColor = backgroundColor;
    CALayer *layer = label.layer;
    
    layer.borderColor = borderColor.CGColor;
    layer.borderWidth = borderWidth;
    layer.cornerRadius = cornerRadius;
    
    CGSize sizeFit = [label sizeThatFits:CGSizeZero];
    label.frame = CGRectMake(0, 0, sizeFit.width + 2 * padding, sizeFit.height + 2 * padding);
    
    return label;
}

#pragma mark - UITextView

//textview
+ (UITextView *)drawTextViewInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font text:(NSString *)text
{
    UITextView *tv = [self getTextViewWithFrame:frame font:font text:text];
    [view addSubview:tv];
    return tv;
}

+ (UITextView *)getTextViewWithFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text
{
    UITextView *tv = [[UITextView alloc] initWithFrame:frame];
    tv.text = text;
    tv.backgroundColor = [UIColor clearColor];
    tv.editable = NO;
    tv.font = font;
    CGRect frame2 = tv.frame;
    frame2.size.height = tv.contentSize.height;
    tv.frame = frame2;
    
    return tv;
}

//textfield
+ (UITextField *)drawTextFieldInView:(UIView *)view frame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder  text:(NSString *)text
{
    UITextField *field = [self getTextFieldWithFrame:frame font:font placeholder:placeholder text:text];
    [view addSubview:field];
    return field;
}

+ (UITextField *)getTextFieldWithFrame:(CGRect)frame font:(UIFont *)font  placeholder:(NSString *)placeholder text:(NSString *)text
{
    UITextField *field = [[UITextField alloc] initWithFrame:frame];
    field.font = font;
    field.placeholder = placeholder;
    field.text = text;
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    field.autocorrectionType = UITextAutocorrectionTypeNo;
    return field;
}

//line
+ (UIView *)drawLineInView:(UIView *)view frame:(CGRect)frame color:(UIColor *)color
{
    UIView *line = [self getLineWithFrame:frame color:color];
    [view addSubview:line];
    return line;
}

+ (UIView *)getLineWithFrame:(CGRect)frame color:(UIColor *)color
{
    UIView *line = [[UIView alloc] initWithFrame:frame];
    line.backgroundColor = color;
    return line;
}

//other
//other
+ (CGFloat)lineWidth
{
    static CGFloat width = 0;
    //    if ( 0 == width ) {
    //
    //        if (2 ==  [[UIScreen mainScreen] scale]) {
    //            width = 0.5;
    //        } else if ( 1 ==  [[UIScreen mainScreen] scale] ) {
    //            width = 1;
    //        } else {
    //            width = 0.5;
    //        }
    //    }
    
    width = 1/[[UIScreen mainScreen] scale];
    return width;
}

+ (CGFloat)textWidth:(NSString *)text font:(UIFont *)font
{
    float textWidth;
    if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        CGRect textRect = [text boundingRectWithSize:CGSizeMake(10000, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];
        textWidth = textRect.size.width;
    } else {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        CGSize textSize = [text boundingRectWithSize:CGSizeMake(10000, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        textWidth = textSize.width;
    }
    return textWidth;
}

+ (CGFloat)textHeight:(NSString *)text font:(UIFont *)font
{
    float textHeight;
    if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        CGRect textRect = [text boundingRectWithSize:CGSizeMake(10000, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];
        textHeight = textRect.size.height;
    } else {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        CGSize textSize = [text boundingRectWithSize:CGSizeMake(10000, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        textHeight = textSize.height;
    }
    return textHeight;
}

#pragma mark - 找到第一响应的view
+ (UIView *)findFirstResponder:(UIView*)view
{
    if (view.isFirstResponder)
    {
        return view;
    }
    
    for (UIView *subView in view.subviews)
    {
        UIView *firstResponder = [self findFirstResponder:subView];
        if (firstResponder != nil)
        {
            return firstResponder;
        }
    }
    
    return nil;
}


+ (UILabel *)getUserIdentityLabel{
    UILabel* statusLabel = [TKUtil createLabel:[TKFont heiti:10.0] color:[UIColor colorWithHex:kMainColor alpha:1.]];
    statusLabel.clipsToBounds = YES;
    statusLabel.layer.cornerRadius = 2;
    statusLabel.textAlignment = NSTextAlignmentCenter;
    statusLabel.hidden = YES;
    return statusLabel;
}

+ (void)userIdentityLabel:(UILabel *)statusLabel  manager_info: (NSString*)manager_info
{
    if ([manager_info length]) {
        statusLabel.text = manager_info;
        statusLabel.textColor = [UIColor whiteColor];
        UIColor *backGroundColor = [UIColor colorWithHex:0xfd907f alpha:1];
        if ([manager_info isEqualToString:@"帮主"]) {
            backGroundColor = [UIColor colorWithHex:0xff8ca6 alpha:1];
        }
        statusLabel.backgroundColor = backGroundColor;
        statusLabel.hidden = NO;
        [statusLabel sizeToFit];
        statusLabel.height = 13;
        statusLabel.width = statusLabel.width + 6;
    } else {
        statusLabel.hidden = YES;
        statusLabel.width = 0;
    }
}

+ (UIImage *)imageWithGradient:(UIImage *)image size:(CGSize)size  startColor:(UIColor *)startColor endColor:(UIColor *)endColor
{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    //    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    //    CGContextDrawImage(context, rect, image.CGImage);
    
    // Create gradient
    NSArray *colors = [NSArray arrayWithObjects:(id)endColor.CGColor, (id)startColor.CGColor, nil];
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)colors, NULL);
    
    // Apply gradient
    //    CGContextClipToMask(context, rect, image.CGImage);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, size.height), 0);
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(space);
    
    return gradientImage;
}

@end


